import axios from 'axios'
import userUrl from '../../util/url'
let url = userUrl + 'api.php/users/'

export default {
    namespaced: true,

    state: {
        ruleForm: {
            gender: "男",
            name: '',
            loginName: '',
            age: '',
            loginKey: '',
            phone: '',
            adress: ''
        },
    },
    mutations: {
        setRuleForm(state) {
            state.ruleForm = {
                gender: "男",
                name: '',
                username: '',
                age: '',
                password: '',
                phone: '',
                adress: '',
            }
        }
    },
    actions: {
        getZc({ state, commit, dispatch }) {  //注册数据
           
            return new Promise((resolve, rejact) => {
                axios({
                    method: 'post',
                    url,
                    data: state.ruleForm
                }).then(res => {
                    commit('setRuleForm')
                    resolve()
                }).catch(rej => {
                    rejact()
                })
            })
        },

        getUsername({ state, commit, dispatch }, payload) {
            return new Promise((resolve, rejact) => {
                axios({
                    method: 'get',
                    url,
                    params: {
                        transform: 1, order: 'id,desc', filter: 'username,eq,' + state.ruleForm.username
                    }
                }).then(res => {
                   
                    if (res.data.users.length) {
                        payload.$message({
                            message: "用户名已被用",
                            type: "warning"
                        })
                    } else {
                        resolve()
                    }

                }).catch(() => {
                    rejact()
                })
            })
        }

    }
}